package com.hexacta.hrs.domain.staffing;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.vectrics.employee.Employee;


public class RequirementCalculatorHandler {
	private static RequirementCalculator calculator;
	
	public RequirementCalculatorHandler() {
		this.calculator =  new RequirementCalculator();
	}
	
	public static RequirementCalculator getCalculator() {
		return calculator;
	}

	public static void setCalculator(RequirementCalculator calculator) {
		RequirementCalculatorHandler.calculator = calculator;
	}

	public List<RequirementResult> calculate (Requirement requirement, Map<Employee, Calendar> employees){
		List<RequirementResult> results = new ArrayList<RequirementResult>();
		for (Employee employee : employees.keySet()) {
			Integer months = resultBetweenDates(requirement.getStartDate(), employees.get(employee));
			RequirementResult requirementResult = calculator.createRequirementResult(employee, requirement, months, employees.get(employee));
			if (requirementResult.getResult()>=60) {
				results.add(requirementResult);
			}
		}
		return results;
	}
	
    public Integer resultBetweenDates(final Calendar begin,final  Calendar end){
    	Calendar lastAssignment = Calendar.getInstance();
    	lastAssignment.setTime(end.getTime());
    	Calendar start = Calendar.getInstance();
    	start.setTime(begin.getTime());
    	if (lastAssignment.before(start)) {
			return 0;
		}else{
			lastAssignment.add(Calendar.MONTH, +1);
			if (lastAssignment.before(start) ) {
				return 1;
			}else{
				lastAssignment.add(Calendar.MONTH, +1);
				if (lastAssignment.before(start) ) {
					return 1;
				}
				else return 3;
			}
			
		}
    }
}
